home *** CD-ROM | disk | FTP | other *** search
/ Aminet 39 / Aminet 39 (2000)(Schatztruhe)[!][Oct 2000].iso / Aminet / game / shoot / Orbit_src.lha / Orbit / source / screenshot.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-07-04  |  2.4 KB  |  93 lines

  1. /*
  2.     Amiga port by Oliver Gantert
  3.  
  4.     27.04.2000 - fixed some compiler warnings
  5.     18.06.2000 - char *screen was changed and later passed
  6.                  to free() - fixed, picture was saved vertically
  7.                  mirrored - fixed, function rewritten.
  8. */
  9. /*
  10.  
  11. ORBIT, a freeware space combat simulator
  12. Copyright (C) 1999  Steve Belczyk <steve1@genesis.nred.ma.us>
  13.  
  14. This program is free software; you can redistribute it and/or
  15. modify it under the terms of the GNU General Public License
  16. as published by the Free Software Foundation; either version 2
  17. of the License, or (at your option) any later version.
  18.  
  19. This program is distributed in the hope that it will be useful,
  20. but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22. GNU General Public License for more details.
  23.  
  24. You should have received a copy of the GNU General Public License
  25. along with this program; if not, write to the Free Software
  26. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  27.  
  28. */
  29.  
  30. #include "orbit.h"
  31.  
  32. void ScreenShot()
  33. {
  34.   char fname[32];
  35.   char *screen,
  36.   *work,
  37.   *line;
  38.   FILE *fd;
  39.   int  x,
  40.   y,
  41.   linelen = ScreenWidth * 3;
  42.  
  43.   /* Construct file name */
  44.   sprintf (fname, "orbit%03d.ppm", screen_shot_num);
  45.  
  46.   /* Allocate space for screen */
  47.   if (screen = (char *)malloc(linelen*ScreenHeight))
  48.   {
  49.     /* Get frame buffer */
  50.     glReadPixels (0, 0, ScreenWidth, ScreenHeight, GL_RGB,
  51.     GL_UNSIGNED_BYTE, screen);
  52.  
  53.     if (line = (char *)malloc(linelen))
  54.     {
  55.       /* Open the file */
  56.       if (fd = fopen(fname, "wb"))
  57.       {
  58.         /* Write the PPM file */
  59.         fprintf (fd, "P6\n%d %d\n255\n", ScreenWidth, ScreenHeight);
  60.  
  61.         work = screen + linelen*ScreenHeight;
  62.         for (y=0; y<ScreenHeight; y++)
  63.         {
  64.           for (x=0; x<linelen; x+=3)
  65.           {
  66.             line[x+2] = 0xff & *--work;
  67.             line[x+1] = 0xff & *--work;
  68.             line[x]   = 0xff & *--work;
  69.           }
  70.           fwrite(line, linelen, 1, fd);
  71.         }
  72.         fclose(fd);
  73.         screen_shot_num++;
  74.         Cprint ("Screen shot saved in %s", fname);
  75.       }
  76.       else
  77.       {
  78.         Cprint ("Can't open screen shot file");
  79.       }
  80.       free(line);
  81.     }
  82.     else
  83.     {
  84.       Cprint ("Can't allocate memory for screen shot");
  85.     }
  86.     free(screen);
  87.   }
  88.   else
  89.   {
  90.     Cprint ("Can't allocate memory for screen shot");
  91.   }
  92. }
  93.